package com.xingfly.repository;

import com.xingfly.model.Event;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by SuperS on 2016/12/7.
 */
public interface EventRepository extends JpaRepository<Event, Long> {
    //根据 Relation 的 type 和 id 查询 Event的 type id
    @Query("SELECT e FROM Event e  WHERE e.objectType = :type AND e.objectId = :id")
    List<Event> getEvensByObjectTypeAndObjectId(@Param("type") int objectType, @Param("id") Long objectId);

    //根据objectType和objectID获取Event
    @Query("SELECT e FROM Event e  WHERE e.objectType = :type AND e.objectId = :id")
    Event find(@Param("type") int type, @Param("id") Long id);

    //根据userId分页获取event列表
    @Query("SELECT e FROM Event e WHERE e.user.id = :id")
    List<Event> findByUserId(@Param("id") Long userId, Pageable pageable);

    //根据objectType和ObjectID删除Event
    @Modifying
    @Transactional
    @Query("DELETE FROM Event e WHERE e.objectType=:objectType AND e.objectId=:objectId")
    void deleteByObjectTypeAndObjectId(@Param("objectType") int type, @Param("objectId") Long id);

    @Query("SELECT e FROM Event e WHERE e.id in :ids")
    List<Event> findEventsByIds(@Param("ids") List<Long> eventIds);
}
